//
//  ButtonCollectionView.m
//  FKGW
//
//  Created by song ce on 2017/2/27.
//  Copyright © 2017年 song ce. All rights reserved.
//

#import "ButtonCollectionView.h"
#import "UIView+Extend.h"
#import "ButtonCollectionCell.h"
@implementation ButtonCollectionView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){
        [self _initSubviews];
    }
    return self;
}

-(void)setDataSoure:(NSArray *)dataSoure{
    _dataSoure= dataSoure;
    _myCollectionView.height = self.height;
    [_myCollectionView reloadData];
}
- (void)_initSubviews{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    //设置每个item的大小
    //    layout.itemSize = CGSizeMake(50,50);
    //每一行之间的最小距离
    layout.minimumLineSpacing = 12;
    //同一行item之间的最小间隙
    layout.minimumInteritemSpacing =12;
    //设置滑动方向
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    UICollectionView *collectionV = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.width, self.height) collectionViewLayout:layout];
      collectionV.showsVerticalScrollIndicator = NO;
    //设置分页
    //    collectionV.pagingEnabled = YES;
    [self addSubview:collectionV];
    collectionV.backgroundColor = [UIColor whiteColor];
    //设置代理
    collectionV.delegate = self;
    collectionV.dataSource = self;
    //注册
    [collectionV registerNib:[UINib nibWithNibName:@"ButtonCollectionCell" bundle:nil] forCellWithReuseIdentifier:@"ButtonCollectionCell"];
    
    _myCollectionView = collectionV;
//      [collectionV registerNib:[UINib nibWithNibName:@"CollectionHeadV" bundle:nil]  forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"CollectionHeadV"];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section;{
    return self.dataSoure.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;{
    ButtonCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ButtonCollectionCell" forIndexPath:indexPath];
    TagCellModel *model = self.dataSoure[indexPath.row];
    model.row = indexPath.row;
    cell.model = model;
    __weak typeof(self) weakSelf = self;
    __weak typeof (cell) weakCell = cell;
    cell.tagClickedBlock = ^(){
        [weakSelf.dataSoure enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            ButtonCollectionCell *item = (ButtonCollectionCell*)[collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForRow:idx inSection:0]];
            if(![weakCell isEqual:item]){
                [item deSelectedItem];
            }else{
                [[NSNotificationCenter defaultCenter]postNotificationName:@"propertySelectedSingle" object:model];
            }
        }];
    };
       return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat with = (Main_Screen_Width*.75-2*15-2*12)/3;
    return CGSizeMake(with, 22);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    UIEdgeInsets edge = UIEdgeInsetsMake(10, 15, 10 ,15);
    return edge;
}

@end
